package cc.yugu.yhdw.webserver.service.hdw;

import cc.yugu.yhdw.common.domain.hdw.ItemCategory;
import cc.yugu.yhdw.common.entity.json.ExtResult;
import cc.yugu.yhdw.common.entity.pagination.Page;
import cc.yugu.yhdw.common.entity.tree.Node;
import cc.yugu.yhdw.common.entity.tree.NodeModel;
import cc.yugu.yhdw.common.utils.YhdwUtils;
import cc.yugu.yhdw.webserver.config.AppConfig;
import cc.yugu.yhdw.webserver.persistence.hdw.ItemCategoryMapper;
import cc.yugu.yhdw.webserver.persistence.hdw.ItemMapper;
import cc.yugu.yhdw.webserver.service.AbstractService;
import org.apache.commons.lang.StringUtils;
import org.codehaus.jackson.JsonEncoding;
import org.codehaus.jackson.JsonGenerator;
import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.io.OutputStream;
import java.util.*;
/**
 * 分类管理业务（商品管理）
 * 
 * @Date:Created in 2018/7/9 14:11
 * @Author:
 * @Modified By: zjn 2018/7/9 14:11
 */ 
@Service
public class ItemCategoryService extends AbstractService {

    @Autowired
    AppConfig config;
    @Autowired
    ItemCategoryMapper categoryMapper;
    @Autowired
    ItemMapper itemMapper;
    /**
    * 查询
    * 
    * @Param: [id]
    * @Return: cc.yugu.yhdw.common.domain.hdw.ItemCategory
    * @Author By: zjn 2018/7/9  14:11
    */ 
    public ItemCategory find(int id) {
        return categoryMapper.find(id);
    }
    /**
    * 分页查询
    * 
    * @Param: [search]
    * @Return: cc.yugu.yhdw.common.entity.pagination.Page
    * @Author By: zjn 2018/7/9  14:13
    */ 
    public Page findPage(ItemCategory search) {
        Page page = search.buildPage();
        page.setTotalItems(categoryMapper.findPageCount(search));
        search.setBeginIndex(page.getOffset());
        page.setResult(categoryMapper.findPageResult(search));
        return page;
    }
    public int insert(ItemCategory entity) {
        return categoryMapper.insert(entity);
    }
    public int update(ItemCategory entity) {
        return categoryMapper.update(entity);
    }
    /**
    * 判断是否与商品有关联
    * 
    * @Param: [categoryId]
    * @Return: cc.yugu.yhdw.common.entity.json.ExtResult
    * @Author By: zjn 2018/7/9  14:13
    */ 
    public ExtResult hasRef(int categoryId) {
        // 根据分类查询 商品名称
        String itemName = itemMapper.hasRecordByProperty("category_id", categoryId);
        if(StringUtils.isEmpty(itemName)) {
            return ExtResult.successResult();
        }
        return ExtResult.failResult(String.format("分类下有子类:%s, 不能删除", itemName));
    }
    /**
    * 删除前判断关联
    * 
    * @Param: [id]
    * @Return: cc.yugu.yhdw.common.entity.json.ExtResult
    * @Author By: zjn 2018/7/9  14:15
    */ 
    public ExtResult delete(int id) {
        ExtResult result = hasRef(id);
        if(!result.isSuccess()) {
            return result;
        }
        categoryMapper.delete(id);
        return result;
    }
    /**
    * 输出商品目录树
    * 
    * @Param: [dummy, stream]
    * @Return: void
    * @Author By: zjn 2018/7/9  14:30
    */ 
    public void tree(String dummy, OutputStream stream) throws IOException {
        List<ItemCategory> categoryList = categoryMapper.findAll();

        List<Node<NodeModel>> roots = new ArrayList<Node<NodeModel>>();
        if(StringUtils.isNotEmpty(dummy)) {
            NodeModel data = new NodeModel();
            Node<NodeModel> root = new Node<NodeModel>(data);
            data.setId(0);
            data.setName(dummy);
            roots.add(root);

            for(ItemCategory topItemCategory : categoryList) {
                NodeModel nodeModel = new NodeModel();
                Node<NodeModel> node = new Node<NodeModel>(nodeModel, root);

                nodeModel.setId(topItemCategory.getId());
                nodeModel.setName(topItemCategory.getCategoryName());
            }

        } else {
            for(ItemCategory topItemCategory : categoryList) {
                NodeModel nodeModel = new NodeModel();
                Node<NodeModel> node = new Node<NodeModel>(nodeModel);

                nodeModel.setId(topItemCategory.getId());
                nodeModel.setName(topItemCategory.getCategoryName());
                roots.add(node);
            }
        }

        ObjectMapper objectMapper = new ObjectMapper();
        JsonGenerator json = objectMapper.getJsonFactory().createJsonGenerator(stream, JsonEncoding.UTF8);
        json.writeStartArray();
        YhdwUtils.writeJson(roots, json);
        json.writeEndArray();

        json.flush();
        json.close();
    }

}
